class APIError < StandardError
  TYPES = {
    "common_error" => {code: 40000, message: "查询失败"},
    "bad_request_error" => {code: 40000, message: "查询失败"},
    "not_login_error" => {code: 40001, message: "你的登录信息已过期，请重新登录。"},
  }.with_indifferent_access

  attr_accessor :type, :message, :code

  def initialize(type: "", message: nil, code: nil)
    @type = type.to_sym
    if type.present?
      @message = message || TYPES[@type][:message]
      @code = TYPES[@type][:code]
    else
      @message = message
      @code = code
    end
    super(message)
  end

  def as_json
    {code: code, message: message}
  end

  # def self.doc(type, message = nil)
  #   {code: TYPES[type][:code], desc: message || TYPES[type][:message]}
  # end
end
